package com.scalar.chiptrack.operations.process;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.utils.SqlQueryMappings;

public class ProcessStep{

        // member variables
        private int m_id = -1;
        private int productId= -1;
        private String m_type = null;
        private String m_name = null;
        private String manufacturingPartNo = null;



	public ProcessStep(){
	}
        
        public ProcessStep (int id, String type, String name) {
            m_id = id;
            m_type = type;
            m_name = name;

        }

        public ProcessStep (int id, String type, String name,String manufacturingPartNo,int productId) {
            m_id = id;
            m_type = type;
            m_name = name;
            this.manufacturingPartNo = manufacturingPartNo;
            this.productId = productId;
        }


        public int getId() {
            return m_id;
        }
        public void setId (int id) {
            m_id = id;
        }
        
        public String getType() {
            return m_type;
        }
        public void setType (String type) {
            m_type = type;
        }
        
        public String getName() {
            return m_name;
        }
        
        public void setName (String name) {
            m_name = name;
        }

       public String getManufacturingPartNo()
      {
          return manufacturingPartNo;
      }


        public void setManufacturingPartNo(String manufacturingPartNo)
       {
          this.manufacturingPartNo = manufacturingPartNo;
        }

       public int getProductId()
  {
      return productId;
  }

  public void setProductId(int productId)
  {
      this.productId = productId;
  }



	public static int getProcessStepID(DBTransaction dbTransaction, String processType, String processTypeName, String comments) throws ScalarException{
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int processStepID = 0;
		boolean status = false;

		try{
			// Get the connection from regular datasource if its not in a manual transaction
            if (dbTransaction == null) {
            	conn = DataSource.getConnection();
			}else {
             	conn = dbTransaction.getConnection();
			}
            
			ps = conn.prepareStatement(SqlQueryMappings.get("operations.process.processStep.insertRecord"));
			ps.setString(1, processType);
			ps.setString(2, processTypeName);
			ps.setString(3, comments);
			ps.executeUpdate();

			ps.close();
			ps = null;
			status = true;

			ps = conn.prepareStatement(SqlQueryMappings.get("operations.process.upload.readIdentity"));
			rs = ps.executeQuery();

			if(rs.next() != false){
				processStepID = rs.getInt("docID");
			}
			
		}catch(SQLException sql){
			sql.printStackTrace();
			throw new ScalarException(" upload failed", sql);

		}finally{

			// Commit/Rollback here it selef -- if its not in a manual transaction
			if (dbTransaction == null) {
            	try {
                	if (conn != null) {
                    	if (status == true) {
                        	conn.commit();
                        }else {
                        	conn.rollback();
						}
					}
              	}catch (SQLException ignore) {}
			}
                        
			if(ps != null){
				try{ ps.close(); }catch(Exception e){}
			}
			if(conn != null){
				try{ conn.close(); }catch(Exception e){}
			}

		}
		return processStepID;

	}

	  //Method Added by Raj vallamsetla for updating process_step table on 11/15/2002
	  public static void updateProcessStepID(DBTransaction dbTransaction,String processType,String processTypeName,String comments,int processStepId) throws ScalarException{
		Connection conn = null;
		PreparedStatement ps = null;
		boolean status = false;

		try{
			// Get the connection from regular datasource if its not in a manual transaction
            if (dbTransaction == null) {
            	conn = DataSource.getConnection();
			}else {
             	conn = dbTransaction.getConnection();
			}
            
			ps = conn.prepareStatement(SqlQueryMappings.get("operations.process.processStep.updateRecord"));
			ps.setString(1,processType);
			ps.setString(2,processTypeName);
			ps.setString(3,comments);
			ps.setInt(4, processStepId);
			ps.executeUpdate();
			status = true;

				
		}catch(SQLException sql){
			sql.printStackTrace();
			throw new ScalarException("Updating ProcessStep table failed ", sql);

		}finally{

			// Commit/Rollback here it selef -- if its not in a manual transaction
			if (dbTransaction == null) {
            	try {
                	if (conn != null) {
                    	if (status == true) {
                        	conn.commit();
                        }else {
                        	conn.rollback();
						}
					}
              	}catch (SQLException ignore) {}
			}
                        
			if(ps != null){
				try{ ps.close(); }catch(Exception e){}
			}
			if(conn != null){
				try{ conn.close(); }catch(Exception e){}
			}

		}
	}



     //Ends Here
	 


	public static String getProcessType(int processStepId) throws ScalarException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String processType = null;
		try {
            
			conn = DataSource.getConnection();
			pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.selectProcessType"));
            pstmt.setInt(1, processStepId);
            
            rs = pstmt.executeQuery();
            
			if(rs.next()) {
				processType = rs.getString("Process_Type");
			}
              
                                         
        }catch (Exception sqlex) {
			sqlex.printStackTrace();
			throw new ScalarException ("Could not get ProcessType from database", sqlex);
		}finally {
			if (rs != null) {
				try { rs.close(); } catch (SQLException excp){}
			}
			if (pstmt != null) {
				try { pstmt.close(); } catch (SQLException excp){}
			}
			if (conn != null) {
				try { conn.close(); } catch (SQLException excp){}
			}
		}
		return processType;         
	}
	
	public static int getPreviousStepId(int processStepID) throws ScalarException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int previousStepID = 0;
		
		try{
			conn = DataSource.getConnection();
			pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.processStep.previousStepID"));
            pstmt.setInt(1, processStepID);
            rs = pstmt.executeQuery();
            
			if(rs.next()) {
				previousStepID = rs.getInt("ProcessStep_ID");
			}
			
		}catch (Exception sqlex) {
		
			sqlex.printStackTrace();
			throw new ScalarException ("Could not get PreviousStep_ID  from database", sqlex);
			
		}finally {
		
			if (rs != null) {
				try { rs.close(); } catch (SQLException excp){}
			}
			if (pstmt != null) {
				try { pstmt.close(); } catch (SQLException excp){}
			}
			if (conn != null) {
				try { conn.close(); } catch (SQLException excp){}
			}
		}
		return previousStepID;
	}
	
	public static ArrayList getNextStepId(int processStepID) throws ScalarException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ArrayList list = new ArrayList();
		
		try{
			conn = DataSource.getConnection();
			pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.processStep.nextStepID"));
            pstmt.setInt(1, processStepID);
            rs = pstmt.executeQuery();
            
			while(rs.next()) {
				list.add(rs.getInt("Next_ProcessStep_ID")+"");
			}
			
		}catch (Exception sqlex) {
		
			sqlex.printStackTrace();
			throw new ScalarException ("Could not get Next_ProcessStep_ID  from database", sqlex);
			
		}finally {
		
			if (rs != null) {
				try { rs.close(); } catch (SQLException excp){}
			}
			if (pstmt != null) {
				try { pstmt.close(); } catch (SQLException excp){}
			}
			if (conn != null) {
				try { conn.close(); } catch (SQLException excp){}
			}
		}
		
		return list;
	}
	
        
        
        // Added by Balaram, Feb 17, 2003
        public static int getProcessStepId (DBTransaction dbTransaction, String partNumber, String stepTypeName) throws ScalarException {
            
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            int processStepId = -1;

            if ((partNumber == null) || (stepTypeName == null)) {
                return processStepId;
            }
            
            try{
                // Get the connection from regular datasource if its not in a manual transaction
                if (dbTransaction == null) {
                    conn = DataSource.getConnection();
                }
                else {
                    conn = dbTransaction.getConnection();
                }
                pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.process.processStep.getProcessStepIdByPartNumberAndStepTypeName"));
                
                pstmt.setString (1, partNumber);
                pstmt.setString (2, stepTypeName);
                
                rs = pstmt.executeQuery();

                if (rs.next()) {
                    processStepId = rs.getInt ("ProcessStep_ID");
                }
            }
            
            catch (Exception sqlex) {
                sqlex.printStackTrace();
                throw new ScalarException ("Could not get Next processStep_ID for PartNumber: " + partNumber + ", Step Type: " + stepTypeName, sqlex);
            }
            
            finally {

                if (rs != null) {
                    try { rs.close(); } catch (SQLException excp){}
                }
                
                if (pstmt != null) {
                    try { pstmt.close(); } catch (SQLException excp){}
                }
                
                if (conn != null) {
                    try { conn.close(); } catch (SQLException excp){}
                }
            }

            return processStepId;
        }
        
        
        
	public static void main(String[] args) throws Exception{
		/*ArrayList list = getNextStepId(56);
		Iterator it = list.iterator();
		while(it.hasNext()){
			System.out.println("StepID = "+(String)it.next());
		}*/
            
            System.out.println ("process StepID: " + getProcessStepId (null, "SS5050", "SORT"));
	}
}